package com.zking.web.LYK;


import com.zking.dao.domain.*;
import com.zking.service.AsDoctorService;
import com.zking.service.RegisterService;
import com.zking.utils.DateUtil;
import com.zking.utils.OrderIdUtil;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class AsDoctorController {

    @Autowired
    AsDoctorService asDoctorService;


    @RequestMapping("showDoctor/{id}")
    public String showDoctor(Model model, @PathVariable Integer id){

        List<Doctor> doctors = asDoctorService.showDoctor(id);
        System.out.println("doctors = " + doctors);
        model.addAttribute("doctors",doctors);
        return "/Front_page/keshiys";
    }


    @RequestMapping("showSchedule/{did}")
    public String showSchedule(Model model,@PathVariable Integer did){

        ArrayList<String> futureDaysList = new ArrayList<>();
        for (int i = 1; i <8; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) +i);
            Date today = calendar.getTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String result = format.format(today);

            futureDaysList.add(result);
        }
        List<Doc_schedule> schedules =asDoctorService.querySchedule(did,futureDaysList);

        System.out.println(schedules);
        System.out.println(futureDaysList);
       Integer shangwu[] = {0,0,0,0,0,0,0};
        for (int i = 0; i < futureDaysList.size(); i++) {

            for (int i1 = 0; i1 < schedules.size(); i1++) {
                if(futureDaysList.get(i).equals(schedules.get(i1).getDaytime())&&(schedules.get(i1).getTime1()!=0||schedules.get(i1).getTime2()!=0||schedules.get(i1).getTime3()!=0||schedules.get(i1).getTime4()!=0)){
                    shangwu[i]=1;
                }



            }
        }

       List<Integer> shangwuList= Arrays.asList(shangwu);

        Integer xiawu[] = {0,0,0,0,0,0,0};
        for (int i = 0; i < futureDaysList.size(); i++) {

            for (int i1 = 0; i1 < schedules.size(); i1++) {
                if(futureDaysList.get(i).equals(schedules.get(i1).getDaytime())&&(schedules.get(i1).getTime5()!=0||schedules.get(i1).getTime6()!=0||schedules.get(i1).getTime7()!=0||schedules.get(i1).getTime8()!=0)){
                    xiawu[i]=1;
                }



            }
        }

        List<Integer> xiawuList= Arrays.asList(xiawu);
        System.out.println(shangwuList);
        System.out.println(xiawuList);

        Doctor doctor = asDoctorService.queryDoctor(did);

        model.addAttribute("doctor",doctor);
        model.addAttribute("futureDaysList",futureDaysList);
        model.addAttribute("schedules",schedules);
        model.addAttribute("shangwuList",shangwuList);
        model.addAttribute("xiawuList",xiawuList);

        return "Front_page/ys";
    }

    @RequestMapping("selectOrder")
    public String  selectOrder(Model model,int time,int did,int day,HttpSession session){

        List<String> futureDaysList = new ArrayList<>();
        for (int i = 1; i <8; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) +i);
            Date today = calendar.getTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String result = format.format(today);

            futureDaysList.add(result);
        }

        String dayTime = futureDaysList.get(day);

        Doctor doctor = asDoctorService.queryDoctor(did);
        Doc_schedule doc_schedule = asDoctorService.selectOrder(dayTime, did);
        if(time==1){
            doc_schedule.setTime5(0);
            doc_schedule.setTime6(0);
            doc_schedule.setTime7(0);
            doc_schedule.setTime8(0);
        }
        else{
            doc_schedule.setTime1(0);
            doc_schedule.setTime2(0);
            doc_schedule.setTime3(0);
            doc_schedule.setTime4(0);
        }

        Department department = asDoctorService.queryDept(Integer.parseInt(doctor.getDeptid()));
        model.addAttribute("doctor",doctor);
        model.addAttribute("docSchedule",doc_schedule);
        model.addAttribute("department",department);
        Patient patient = (Patient) session.getAttribute("msg");

        model.addAttribute("patient",patient);
        System.out.println(doctor);
        System.out.println(doc_schedule);
        return "Front_page/selectOrder";

    }

    @RequestMapping("/pay")
    public String pay(Model model,int docId ,String day,int deptId,String time, HttpSession session){
        Doctor doctor = asDoctorService.queryDoctor(docId);
        Department department = asDoctorService.queryDept(Integer.parseInt(doctor.getDeptid()));
        System.out.println(time);
        System.out.println(day);
        String dayTime = null;
        if(time.equals("time1")){
            time="time_1";
            dayTime="8:00-9:00";
        }
        else  if(time.equals("time2")){
            time="time_2";
            dayTime="9:00-10:00";
        }
        else  if(time.equals("time3")){
            time="time_3";
            dayTime="10:00-11:00";
        }
        else  if(time.equals("time4")){
            time="time_4";
            dayTime="11:00-12:00";
        }
        else  if(time.equals("time5")){
            time="time_5";
            dayTime="14:00-15:00";
        }
        else  if(time.equals("time6")){
            time="time_6";
            dayTime="15:00-16:00";
        }
        else  if(time.equals("time7")){
            time="time_7";
            dayTime="16:00-17:00";
        }
        else  if(time.equals("time8")){
            time="time_8";
            dayTime="17:00-18:00";
        }

        asDoctorService.updateSchedule(time,doctor.getDname(),day);

        String order_id= OrderIdUtil.getOrderIdByTime();
        model.addAttribute("order_id", order_id);
        model.addAttribute("day",day);
        model.addAttribute("doctor",doctor);
        model.addAttribute("dept",department);
        model.addAttribute("time",dayTime);
        String create_time=day+" " +dayTime;
        Patient patient= (Patient) session.getAttribute("msg");

        String date =DateUtil.getTime();
        asDoctorService.insertPay(date,doctor.getPrice());
        asDoctorService.finishOrder(doctor.getId(),department.getId(),patient.getId(),create_time,order_id);
        return "Front_page/pay";
    }

    @RequestMapping(value = "/tradePay",method = RequestMethod.POST)
    public String tradePay(){

        return "Front_page/alipay.trade.page.pay";
    }




    @RequestMapping("/finishOrder")
    public String finishOrder(String out_trade_no){

        String order_id =out_trade_no;
        System.out.println(order_id);
        asDoctorService.finishPay(order_id);

        return "redirect:/toIndex";

    }


    @RequestMapping("/searchOrder")
    @ResponseBody
    public List<Object> searchOrder(String phone){

        List<Object> orders = new ArrayList<>();
        Patient patient =asDoctorService.queryPatientByPhone(phone);
        System.out.println("这个是电话"+phone);
        System.out.println("patient = " + patient);

            List<Register> registers = asDoctorService.queryRegister(patient.getId());
            System.out.println(registers);
            if(registers!=null){
                orders.add(registers);
            }
            else{
                orders.add("没有此用户的信息");
            }





        return orders;


    }

}
